package com.fuyd.pms.background.service;

import com.fuyd.pms.background.repository.RegionDao;
import com.fuyd.pms.dto.v1.City;
import com.fuyd.pms.dto.v1.District;
import com.fuyd.pms.dto.v1.Province;
import com.fuyd.pms.dto.v1.RegionDto;
import com.fuyd.pms.entity.Region;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 地区管理的service
 * Created by fuyongde on 2016/1/13.
 */
@Service
public class RegionService {

    /**
     * 地区的Dao
     **/
    private RegionDao regionDao;

    @Autowired
    public void setRegionDao(RegionDao regionDao) {
        this.regionDao = regionDao;
    }

    /**
     * 根据id查询地区
     *
     * @param id 地区id
     * @return 地区信息
     */
    public RegionDto findById(String id) {
        Region region = regionDao.findOne(id);
        return region2RegionDto(region);
    }

    /**
     * 根据父级id查询地区列表
     *
     * @param parentId 父级id
     * @return 地区列表
     */
    public List<RegionDto> findByParentId(String parentId) {
        List<Region> regionList = regionDao.findByParentId(parentId);
        List<RegionDto> regionDtos = Lists.newArrayList();
        if (regionList != null && regionList.size() > 0) {
            for (Region region : regionList) {
                regionDtos.add(region2RegionDto(region));
            }
        }
        return regionDtos;
    }

    /**
     * 查询所有的省市区
     *
     * @return 地区列表
     */
    public List<Province> findAll() {
        //循环里面嵌套SQL是不合适的，通常的做法应该是先将数据全部取出来，然后分析，不过犹豫该方法单纯为了跑出一个json文件，故不care那些细节
        List<Region> provinceList = regionDao.findByParentId("0");
        List<Province> provinces = Lists.newArrayList();
        for (Region provinceRegion : provinceList) {
            String provinceId = provinceRegion.getId();
            List<City> cities = Lists.newArrayList();
            List<Region> cityList = regionDao.findByParentId(provinceId);
            for (Region cityRegion : cityList) {
                String cityId = cityRegion.getId();
                List<District> districts = Lists.newArrayList();
                List<Region> districtList = regionDao.findByParentId(cityId);
                for (Region districtRegion : districtList) {
                    districts.add(region2District(districtRegion));
                }
                cities.add(region2City(cityRegion, districts));
            }
            provinces.add(region2Province(provinceRegion, cities));
        }
        return provinces;
    }

    /**
     * Region转RegionDto
     *
     * @param region 地区信息
     * @return com.fuyd.pms.dto.v1.RegionDto
     */
    private static RegionDto region2RegionDto(Region region) {
        if (region == null) {
            return null;
        }
        RegionDto regionDto = new RegionDto();
        regionDto.setCode(region.getId());
        regionDto.setName(region.getName());
        regionDto.setLevel(region.getLevel());
        return regionDto;
    }

    /**
     * 地区信息转区域信息
     *
     * @param region 地区信息
     * @return com.fuyd.pms.dto.v1.District
     */
    private static District region2District(Region region) {
        if (region == null) {
            return null;
        }
        District district = new District();
        district.setCode(region.getId());
        district.setName(region.getName());
        district.setLevel(region.getLevel());
        return district;
    }

    /**
     * 地区信息转城市信息
     *
     * @param region 地区信息
     * @return com.fuyd.pms.dto.v1.City
     */
    private static City region2City(Region region, List<District> districtList) {
        if (region == null) {
            return null;
        }
        City city = new City();
        city.setCode(region.getId());
        city.setName(region.getName());
        city.setLevel(region.getLevel());
        city.setDistricts(districtList);
        return city;
    }

    /**
     * 地区信息转省份信息
     *
     * @param region 地区信息
     * @return com.fuyd.pms.dto.v1.Province
     */
    private static Province region2Province(Region region, List<City> cityList) {
        if (region == null) {
            return null;
        }
        Province province = new Province();
        province.setCode(region.getId());
        province.setName(region.getName());
        province.setLevel(region.getLevel());
        province.setCities(cityList);
        return province;
    }
}
